package org.v2ray.rocket.proxy.support.utils

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator

object AnimationUtils {
    
    fun fadeIn(view: View, duration: Long = 300, onEnd: (() -> Unit)? = null) {
        view.alpha = 0f
        view.visibility = View.VISIBLE
        
        val animator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).apply {
            this.duration = duration
            interpolator = AccelerateDecelerateInterpolator()
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    onEnd?.invoke()
                }
            })
        }
        animator.start()
    }
    
    fun fadeOut(view: View, duration: Long = 300, onEnd: (() -> Unit)? = null) {
        val animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f).apply {
            this.duration = duration
            interpolator = AccelerateDecelerateInterpolator()
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    view.visibility = View.GONE
                    view.alpha = 1f
                    onEnd?.invoke()
                }
            })
        }
        animator.start()
    }
    
    fun slideInFromBottom(view: View, duration: Long = 300, onEnd: (() -> Unit)? = null) {
        view.alpha = 0f
        view.translationY = 50f
        view.visibility = View.VISIBLE
        
        val alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)
        val translationAnimator = ObjectAnimator.ofFloat(view, "translationY", 50f, 0f)
        
        alphaAnimator.duration = duration
        translationAnimator.duration = duration
        alphaAnimator.interpolator = AccelerateDecelerateInterpolator()
        translationAnimator.interpolator = AccelerateDecelerateInterpolator()
        
        alphaAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                onEnd?.invoke()
            }
        })
        
        alphaAnimator.start()
        translationAnimator.start()
    }
    
    fun slideOutToBottom(view: View, duration: Long = 300, onEnd: (() -> Unit)? = null) {
        val alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f)
        val translationAnimator = ObjectAnimator.ofFloat(view, "translationY", 0f, 50f)
        
        alphaAnimator.duration = duration
        translationAnimator.duration = duration
        alphaAnimator.interpolator = AccelerateDecelerateInterpolator()
        translationAnimator.interpolator = AccelerateDecelerateInterpolator()
        
        alphaAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                view.visibility = View.GONE
                view.alpha = 1f
                view.translationY = 0f
                onEnd?.invoke()
            }
        })
        
        alphaAnimator.start()
        translationAnimator.start()
    }
    
    fun crossSlide(slideOutView: View, slideInView: View, duration: Long = 300, onEnd: (() -> Unit)? = null) {
        slideOutToBottom(slideOutView, duration) {
            slideInFromBottom(slideInView, duration, onEnd)
        }
    }
}
